<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.request.DBUtils"%>
<%@ page import="com.request.Request"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>

<html>
<head>
<title>Admin Console</title>
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>

	<%
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user == null) {
			response.sendRedirect(userService.createLoginURL(request
					.getRequestURI()));
		}

		if (!userService.isUserAdmin()) {
			response.sendRedirect("nonadmin.jsp");
		}
	%>
	<legend>Administrator Portal-List of Book Requests</legend>

	<br />

	<form class="well form-inline" method="post" action="admin.jsp">
		<label class="control-label" for="sort">Sort Preference</label> <select
			name="sort">
			<option value="requestdate desc">RequestDate Descending</option>
			<option value="requestdate asc">RequestDate Ascending</option>
			<option value="university asc">University Ascending</option>
			<option value="university desc">University Descending</option>
		</select>
		<button type="submit" class="btn btn-primary">Go</button>
	</form>

	<%
		String sort = request.getParameter("sort");
		if (sort == "" || sort == null)
			sort = "requestdate desc";
		List<Request> result = new ArrayList<Request>();
		result = DBUtils.listrequest(sort);
	%>

	<table class="table table-striped">
		<thead>
			<tr>
				<th>Sno</th>
				<th>Name</th>
				<th>Email</th>
				<th>Company/University</th>
				<th>Request Date</th>
				<th>Comments/Feedback</th>
				<th>Action</th>
			</tr>
		</thead>

		<%
			for (int i = 0; i < result.size(); i++) {
		%>
		<tr>
			<td><%=i + 1%></td>
			<td><%=result.get(i).getName()%></td>
			<td><%=result.get(i).getEmail()%></td>
			<td><%=result.get(i).getUniversity()%></td>
			<td><%=result.get(i).getRequestdate()%></td>
			<td><%=result.get(i).getComment()%></td>
			<td><a href="edit.jsp?id=<%=result.get(i).getId()%>">Edit</a> |
				<a href="delete.jsp?id=<%=result.get(i).getId()%>">Delete</a></td>
		</tr>
		<%
			}
		%>

	</table>

	<form name="frm" method="post"
		action="<%=userService.createLogoutURL(request.getRequestURI())%>">
		<fieldset>
			<div class="form-actions">
				<button type="submit" class="btn btn-primary">Logout</button>
			</div>
		</fieldset>
	</form>

</body>
</html>